package org.jeecg.common.util.tencent;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.IMJsonUtil;
import org.jeecg.common.util.enums.centent.IMApiEnum;
import org.jeecg.common.util.tencent.response.IMBaseResponse;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

/**
 * @author guoyh
 * @ClassName ImUtil
 * @Description
 * @since 2020/8/22 23:28
 **/
@Slf4j
public class ImUtil {

    private static final String URL_FORMAT = "https://console.tim.qq.com/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%s&contenttype=json";

    private static final String SIGN = "eJw1jrEOgjAURf+ls8E+CkRIHAgGHbqgDDoSWuBJqE2ppsT47xLQu917cpL7JiW/eNJpNJIkDEIW0TmbZX5JQxLie5SsfRR9pTUKkkBAaQDAgmglKKSy2OAiVGJAhaM1lX2Yv4rtTJiaDo7Hx2y88rSMb13z1CbtYUrvNa+L7dmdZN61mBf1/idaHOZjEMY7AMpC//MFW4A1bw==";

    private String ver;

    private Long appid;

    private String identifier;

    private SimpleClientHttpRequestFactory clientHttpRequestFactory;

    public ImUtil(String ver,Long appid,String identifier){
        this.ver = ver;
        this.appid = appid;
        this.identifier = identifier;
        clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        clientHttpRequestFactory.setReadTimeout(10000);
        clientHttpRequestFactory.setConnectTimeout(5000);
    }


    public <T> T sendRequest(IMApiEnum apiEnum, Object param,Class<T> clazz){
        log.info("\n++++++++++++++聊天室请求的数据+++++++++++++++++++\n"+param+"\n++++++++++++++++++++++++++++++++++++\n");

        Random random = new Random();
        int ran = random.nextInt(Integer.MAX_VALUE);
        String url = String.format(URL_FORMAT,ver,apiEnum.getPath(),appid,identifier,SIGN,ran);
        String jsonParam = IMJsonUtil.getJson(param);
        if (log.isDebugEnabled()){
            log.debug("请求腾讯IM，请求地址：{},参数：{}",url, jsonParam);
        }
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Object> httpEntity = new HttpEntity<>(jsonParam,requestHeaders);
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        ResponseEntity<T> responseEntity = restTemplate.postForEntity(url,httpEntity,clazz);
        if(!responseEntity.getStatusCode().is2xxSuccessful()){
           log.error("调用腾讯IM接口返回异常，状态码：{},请求url：{},参数：{}",responseEntity.getStatusCodeValue(),url,jsonParam);
        }
        if (log.isDebugEnabled()){
            log.debug("请求腾讯IM，响应结果：{}", JSONUtil.toJsonStr(responseEntity.getBody()));
        }

        log.info("\n++++++++++++++腾讯IM请求的结果+++++++++++++++++++\n"+responseEntity.getBody()+"\n++++++++++++++++++++++++++++++++++++\n");
        return responseEntity.getBody();
    }
}
